open Common

class dlgResources () = object(self)
	method caption = "Setup movie's info"

	method text1 = "Enter movie's title and year"
	method text2 = "Enter movie's description"
end
	
class addMovieDlg () = object(self)
	val resources = new dlgResources()
	val _titleEdit = GEdit.entry ()
	val _yearEdit = GEdit.spin_button()
	val _descrBuffer = GText.buffer()
	val _wnd = GWindow.dialog ~modal:true ()

	initializer
		_yearEdit#set_adjustment
			(GData.adjustment
				 ~value:2009.0
				 ~lower:1900.0
				 ~upper:2021.0 ());
		
		_yearEdit#set_numeric true;
		_yearEdit#set_update_policy `IF_VALID;

		self#initialize()
			
			
	method private initialize() =
		_wnd#set_title resources#caption;
		_wnd#add_button_stock `OK `OK;
		_wnd#add_button_stock `CANCEL `CANCEL;
		_wnd#set_default_response `CANCEL;
		
		let vb = GPack.vbox ~packing:(_wnd#vbox#add) () in
		let fbox = GPack.vbox ~packing:vb#pack () in
			GMisc.label ~text:resources#text1 ~packing:(fbox#pack ~from:`START) () |> ignore;
			let fboxInner = GPack.hbox ~packing:(fbox#pack) () in
				fboxInner#pack _titleEdit#coerce ~expand:true;
				fboxInner#pack _yearEdit#coerce;
				let sbox = GPack.vbox ~packing:(vb#pack ~expand:true ~fill:true) () in
					GMisc.label  ~text:resources#text2 ~packing:(sbox#pack ~from:`START) () |> ignore;
					let sc_win = GBin.scrolled_window
						~shadow_type:`NONE
						~hpolicy:`NEVER
						~vpolicy:`AUTOMATIC
						~packing:(sbox#pack ~expand:true ~fill:true)()
					in
						GText.view ~buffer:_descrBuffer ~packing:sc_win#add ~editable:true () |> ignore;

	method private prepare_dialog (movie : MovieDto.movie option) =
		match movie with
			| Some(m) -> 
					_titleEdit#set_text m#title;
					_yearEdit#set_value (float_of_int m#year);
					_descrBuffer#set_text m#desc;
			| None ->
					_titleEdit#set_text "";
					_yearEdit#set_value 2009.0;
					_descrBuffer#set_text "";
						
	method run movie  =
		self#prepare_dialog movie;
		let res = match _wnd#run() with
			| `OK -> Some(
					new MovieDto.movie ~title:_titleEdit#text  ~year:_yearEdit#value_as_int ~desc:(_descrBuffer#get_text()) ()
				)
			| _  -> None
		in
			_wnd#misc#hide();
			res
end
